#-*- coding: utf-8 -*-

import simpleform as sf
import re


class LinkDeleteConfirmForm(sf.Form):
	confirmtext = sf.WarningTextField(u'确认删除这个链接吗？')
	confirmed = sf.BoolField(label=u'', initial=True, hidden=True)
	
	def validate(self):
		if self.request.POST:
			data = self.data
			if data['confirmed']:
				return True
		return False

class LinkEditForm(sf.Form):
	label = sf.Field(label='链接标签', widget=sf.TextWidget(attrs={'size':30}),
		help_text='* 必填')
	url  = sf.Field(label='链接目标Url', widget=sf.TextWidget(attrs={'size':45}),
		help_text='* 以http://等协议名开头')
	linkdesc  = sf.Field(label='描述', widget=sf.TextWidget(attrs={'size':45}))
	listindex = sf.IntField(label='排序位置', widget=sf.TextWidget(attrs={'size':3}))
	highlight = sf.BoolField(label='加亮显示', widget=sf.SingleChoiceWidget)
	
	def validate(self):
		if self.request.POST:
			data = self.data
			if not data['label']:
				self.errors['label'] = u'请填写链接标签文字。'
				return False
			elif not data['url']:
				self.errors['url'] = u'请填写链接目标Url地址。'
				return False
			return True
		return False


class CategoryDeleteConfirmForm(sf.Form):
	confirmtext = sf.WarningTextField(u'确认删除这个栏目吗？删除栏目并不会删除关联文章。')
	confirmed = sf.BoolField(label=u'', initial=True, hidden=True)
	
	def validate(self):
		if self.request.POST:
			data = self.data
			if data['confirmed']:
				return True
		return False


class CategorySettingsForm(sf.Form):
	categoryname = sf.Field(label='栏目名称')
	listindex = sf.IntField(label='排序位置', widget=sf.TextWidget(attrs={'size':3}))
	
	def validate(self):
		if self.request.POST:
			data = self.data
			if not data['categoryname']:
				self.errors['categoryname'] = u'请填写栏目名称。'
				return False
			return True
		return False

class CommentDeleteConfirmForm(sf.Form):
	confirmtext = sf.WarningTextField(u'确认删除这条评论吗？')
	confirmed = sf.BoolField(label=u'', initial=True, hidden=True)
	
	def validate(self):
		if self.request.POST:
			data = self.data
			if data['confirmed']:
				return True
		return False


class PostDeleteConfirmForm(sf.Form):
	confirmtext = sf.WarningTextField(u'确认删除这篇文章吗？')
	confirmed = sf.BoolField(label=u'', initial=True, hidden=True)
	
	def validate(self):
		if self.request.POST:
			data = self.data
			if data['confirmed']:
				return True
		return False

class PostEditForm(sf.Form):
	subject = sf.Field(label='标题', widget=sf.TextWidget(attrs={'size':35}), 
		help_text='* 必填')
	content = sf.Field(label='内容正文', widget=sf.HtmlEditorWidget(attrs={
			'uploadchannel': '', 'uploadurl': '/admin/media/upload/', 
			'width':'100%', 'height': '320px'
		}))
	categoryid = sf.LongField(label='文章分类', widget=sf.SingleChoiceWidget, 
		choices=((0, '默认'),))
	isdraft = sf.BoolField(label='发布状态', widget=sf.RadioGroupWidget, 
		choices=((False, u'立即发布'),(True, u'草稿'),))
	
	def validate(self):
		if self.request.POST:
			data = self.data
			if not data['subject']:
				self.errors['subject'] = u'请填写标题。'
				return False
			return True
		return False


class PasswordUpdateForm(sf.Form):
	current_password = sf.Field(label='当前密码', widget=sf.PasswordWidget)
	new_password = sf.Field(label='新密码', widget=sf.PasswordWidget)
	new_password_repeat = sf.Field(label='重复输入新密码', widget=sf.PasswordWidget)

	def validate(self):
		if self.request.POST:
			data = self.data
			if not data['current_password']:
				self.errors['current_password'] = u'当前密码必须填写。'
				return False
			elif not data['new_password']:
				self.errors['new_password'] = u'请填写新的登录密码。'
				return False
			elif not data['new_password_repeat']:
				self.errors['new_password_repeat'] = u'请重复输入一次新的登录密码。'
				return False
			elif data['new_password'] != data['new_password_repeat']:
				self.errors['new_password'] = u'请确认新密码的两次输入完全一致。'
				return False
			return True
		return False


class DeleteUserConfirmForm(sf.Form):
	'''this form is for root user management usage.'''
	confirmtext = sf.WarningTextField(u'确认删除这个用户吗？')
	confirmed = sf.BoolField(label=u'', initial=True, hidden=True)
	
	def validate(self):
		if self.request.POST:
			data = self.data
			if data['confirmed']:
				return True
		return False

class UserPasswordUpdateForm(sf.Form):
	'''this form is for root user management usage.'''
	new_password = sf.Field(label='新密码', widget=sf.PasswordWidget)
	new_password_repeat = sf.Field(label='重复输入新密码', widget=sf.PasswordWidget)
	
	def validate(self):
		if self.request.POST:
			data = self.data
			if not data['new_password']:
				self.errors['new_password'] = u'请填写新的登录密码。'
				return False
			elif not data['new_password_repeat']:
				self.errors['new_password_repeat'] = u'请重复输入一次新的登录密码。'
				return False
			elif data['new_password'] != data['new_password_repeat']:
				self.errors['new_password'] = u'请确认新密码的两次输入完全一致。'
				return False
			return True
		return False

class UserRoleAndStatusUpdatingForm(sf.Form):
	'''this form is for root user management usage.'''
	domainname = sf.Field(label='子域名/目录', help_text='* 仅允许字母和数字')
	isroot = sf.BoolField(label='是否管理员', choices=((True, u'是'),(False, '否')),
		widget=sf.SingleChoiceWidget, initial=False)
	enabled = sf.BoolField(label='允许登录', choices=((True, u'是'),(False, '否')),
		widget=sf.SingleChoiceWidget, initial=True)
	
	def validate(self):
		if self.request.POST:
			data = self.data
			if data['domainname']:
				if not re.match(r'^[\d\w]+$', data['domainname']):
					self.errors['domainname'] = u'如果设置子域名则必须符合规则，只允许字母和数字。'
					return False
			return True
		return False

class UserSettingsForm(sf.Form):
	domainname = sf.Field(label='子域名/目录', help_text='* 仅允许字母和数字')
	email = sf.Field(label='电子邮件', help_text='* 可用于找回忘记的密码')
	namealias = sf.Field(label='显示昵称', widget=sf.TextWidget(attrs={'size':12}))
	fullname = sf.Field(label='姓名', widget=sf.TextWidget(attrs={'size':12}))
	theme = sf.Field(label='模板样式', widget=sf.SingleChoiceWidget, choices=(('','默认'),))

	def validate(self):
		if self.request.POST:
			data = self.data
			if data['domainname']:
				if not re.match(r'^[\d\w]+$', data['domainname']):
					self.errors['domainname'] = u'如果设置子域名则必须符合规则，只允许字母和数字。'
					return False
			return True
		return False

class SignInForm(sf.Form):
	
	username = sf.Field(label=u'用户名')
	password = sf.Field(label=u'密码', widget=sf.PasswordWidget)
	
	def validate(self):
		if self.request.POST:
			data = self.data
			if not data['username']:
				self.errors['username'] = u'请填写用户名。'
			if not data['password']:
				self.errors['password'] = u'请填写登录密码。'
			return len(self.errors)<=0
		return False



